<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Services\Admin\AdminService;
use App\Services\Admin\PermissionService;
use App\Services\Admin\RoleService;
use Illuminate\Http\Request;

class UserController extends Controller
{
    protected $request;
    protected $user;
    protected $adminService;
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct(Request $request, AdminService $adminService)
    {
        $this->request = $request;
        $this->user = $adminService->getUserInfo($request->get('currentUserId'));
        $this->adminService = $adminService;
    }

    /**
     * 获取用户信息
     * @param AdminService $adminService
     * @return \Illuminate\Http\JsonResponse
     */
    public function getUserInfo(){
        return success('ok',$this->user);
    }

    /**
     * 获取权限码
     * @return array
     */
    public function getPermCode(PermissionService $permissionService){
        $result = $permissionService->getPermCode();
        return $result;
    }

    /**
     * 获取菜单列表
     * @param PermissionService $permissionService
     * @return \Illuminate\Http\JsonResponse
     */
    public function getMenuList(PermissionService $permissionService){
        $result = $permissionService->getMenuList($this->user->role_id);
        return $result;
    }

    /**
     * 获取账户列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function getAccountList(){
        $params = $this->request->input();
        $result = $this->adminService->getAccountList($params);
        return $result;
    }

    /**
     * 设置管理员状态
     */
    public function setAdminStatus(){
        $params = $this->request->input();
        $result = $this->adminService->setAdminStatus($params);
        return $result;
    }

    /**
     * 获取所有角色
     * @param PermissionService $permissionService
     * @return \Illuminate\Http\JsonResponse
     */
    public function getAllRoleList(RoleService $roleService){
        $result = $roleService->getAllRoleList();
        return $result;
    }

    /**
     * 验证用户名是否存在
     * @return \Illuminate\Http\JsonResponse
     */
    public function accountExist(){
        $result = $this->adminService->accountExist($this->request->post('username'));
        return $result;
    }

    /**
     * 新建管理员
     * @return \Illuminate\Http\JsonResponse
     */
    public function createOrUpdateAccount(){
        $this->validate($this->request, [
            'role_id' => 'required',
            'username' => 'required',
            'nickname' => 'required'
        ]);
        $result = $this->adminService->createOrUpdateAccount($this->request->post());
        return $result;
    }

    /**
     * 登出
     * @return \Illuminate\Http\JsonResponse
     */
    public function logout(){
        $result = $this->adminService->logout();
        return $result;
    }


}
